<?php
/**
 * 权限
 */
namespace app\model;

use think\Model;
use app\libs\Predis;

class CityModel extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'city';
    
    // 设置字段信息
    protected $schema111 = [
        'id'          => 'int',
        'city_name'        => 'string',
    ];
    
    // 设置当前模型的数据库连接
    protected $connection ='db_yunduan';
   
   /**
    * 递归
    */
    public function GetInfo($id=0){

        $cache = new Predis();
        $data=$cache->get('CityDiGui_'.$id);
        if($data){
            $result=unserialize($data);
        }else{
            $city = new CityModel();
            $res=$city->field('*')->where(['status'=>1])->select()->toArray();
    
            $result=arr2tree($res,$id,'city_name','id');
            $cache->set('CityDiGui_'.$id,serialize($result),3600);
        }
        return $result;        
    }
    /**
    *下级
    */
    public function GetChildren($id=0){

        $cache = new Predis();
        $data=$cache->get('CityChildren_'.$id);
        if($data){
            $result=unserialize($data);
        }else{
            $city = new CityModel();
            $result=$city->field('id,city_name,pid')->where(['status'=>1,'pid'=>$id])->select()->toArray();
            $cache->set('CityChildren_'.$id,serialize($result),3600);
        }
        return $result;        
    }
    /**
     * 城市详情
     */
    public function GetDetail($id=0){
    	
    	$city = new CityModel();
    	$res=$city->field('*')->where(['status'=>1])->find($id);  	
    	return $res;
    }
    
    /**
     * 城市详情
     */
    public function GetCityNameByCityIds($arr){
    	if(empty($arr)){
    		return  [];
    	}
    	$param=implode(',',$arr);
    	$city = new CityModel();
    	$res=$city->where("status=1 and id in ($param)")->column('city_name');
    	
    	return $res;
    }
    

}
 